<template>
  <div>
    <div v-if="detail?.reply" class="reply-list">
      <div
        v-for="val in detail.reply"
        :key="val.id"
        class="reply-item"
        @mouseenter="handleMouseenter(val)"
        @mouseleave="handleMouseleave"
      >
        <div>
          <el-avatar
            style="background: transparent"
            :size="30"
            shape="square"
            cross-origin
            :src="formatUrl(val.createAgent?.headPhoto)"
          />
        </div>
        <div class="reply-item__desc">
          <div class="df-alc-jcsb">
            <div>
              <span class="reply-name">{{ val.createAgent?.name }}</span>
              <span class="reply-time">{{ val.createTime }}</span>
            </div>

            <div
              v-if="val.createAgent?.uid === uid && currentReply?.id === val.id"
              class="reply-operator"
            >
              <el-icon :size="20" @click="editReply(val)">
                <edit />
              </el-icon>
              <el-icon :size="20" @click="delReply(val.id)">
                <delete />
              </el-icon>
            </div>
          </div>
          <div class="reply-item__content">
            <div v-if="showEdit && editReplyId === val.id">
              <el-input v-model="replyEdit" type="textarea" />
              <div class="reply-btn">
                <el-button size="small" @click="cancelReply">取消</el-button>
                <el-button type="primary" size="small" @click="editSubmit(val)">确认</el-button>
              </div>
            </div>
            <span v-else>{{ val.reply }}</span>
          </div>
        </div>
      </div>
    </div>

    <div class="reply-input">
      <el-input v-model="reply" type="textarea" :autosize="{ minRows: 8 }" />
      <div class="reply-btn">
        <el-button type="primary" size="small" @click="submit">回复</el-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { defineEmits, defineProps, PropType, ref } from 'vue'

import { ElMessage } from 'element-plus'

import IssuesApi from '@/api/issues'

import { mapGetters } from '@/store/lib'

import { formatUrl } from '@/utils/common/formatUrl'
import { useConfirm } from '@/utils/common/useConfirm'

import IBaseObj from '#/index'

const { uid } = mapGetters()
const props = defineProps({
  info: {
    type: Object as PropType<IBaseObj>,
    default: () => {},
  },
  detail: {
    type: Object as PropType<IBaseObj>,
    default: () => {},
  },
})

const emits = defineEmits(['search', 'search-reply'])

const reply = ref('')
const showEdit = ref(false)
const replyEdit = ref('')
const currentReply: IBaseObj = ref({})
const editReplyId = ref(-1) // 当前点击了编辑的

const handleMouseenter = (val: IBaseObj) => {
  currentReply.value = val
}

const handleMouseleave = () => {
  if (showEdit.value) {
    return
  }
  currentReply.value = {}
  showEdit.value = false
}

const submit = async () => {
  const ret = await IssuesApi.replyIssues({
    vid: props.info.vid,
    isuId: props.detail?.isuId,
    reply: reply.value,
    createUser: uid,
  })
  if (ret) {
    reply.value = ''
    ElMessage({ message: '回复成功！', type: 'success' })
    emits('search')
  }
}

const editReply = (val: IBaseObj) => {
  replyEdit.value = val.reply
  showEdit.value = true
  editReplyId.value = val.id
}

const editSubmit = async (val: IBaseObj) => {
  const ret = await IssuesApi.updateReply({
    id: val.id,
    reply: replyEdit.value,
  })
  if (ret) {
    ElMessage({ message: '修改成功！', type: 'success' })
    currentReply.value.reply = replyEdit.value
    showEdit.value = false
    emits('search-reply')
  }
}

const cancelReply = () => {
  replyEdit.value = ''
  showEdit.value = false
}

const delReply = (id: number) => {
  useConfirm(
    {
      message: '是否删除该回复？',
    },
    async () => {
      const ret = await IssuesApi.deleteReply(id)
      if (ret) {
        ElMessage({ message: '删除成功！', type: 'success' })
        emits('search-reply')
      }
    }
  )
}
</script>

<style lang="scss" scoped>
.reply {
  &-item {
    display: flex;
    padding-bottom: 10px;
    margin-bottom: 10px;
    cursor: pointer;

    &__content {
      margin-top: 3px;
    }

    &__desc {
      flex: 1;
      margin-left: 10px;
    }

    &:hover {
      .reply-operator {
        display: block;
      }
    }
  }

  &-name {
    margin-right: 10px;
    font-weight: 600;
  }

  &-time {
    font-size: 13px;
    color: var(--color-2);
  }

  &-btn {
    margin-top: 10px;
    text-align: right;
  }

  &-operator {
    // display: none;
    .el-icon {
      margin-left: 10px;

      &:hover {
        color: var(--color-primary);
      }
    }
  }
}

.df-alc-jcsb {
  display: flex;
  height: 20px;
  align-items: center;
  justify-content: space-between;
}
</style>
